﻿using System.Reflection;

namespace Sidvall.Serialization
{
	public static class EnumExtension
	{
		#region GetDescription

		public static string GetDescription<T>(this T value) where T : struct
		{
			var enumType = typeof(T);
			var key = value.ToString();
			var fieldInfo = enumType.GetTypeInfo().GetDeclaredField(key);
			var attribute = fieldInfo.GetCustomAttribute(typeof(Sidvall.Serialization.DescriptionAttribute), false);
			if (attribute != null)
				return ((Sidvall.Serialization.DescriptionAttribute)attribute).Description;
			return key;
		}

		#endregion
	}
}
